(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  if (!window.Moshu) {
    window.Moshu = new Object();
  }

  window.Moshu.Canvas2D.Components = new Object();

  Moshu.Canvas2D.Components.AnimationStep = (function(_super) {

    __extends(AnimationStep, _super);

    function AnimationStep() {
      this.flip = false;
      this.steps = [];
      this.continous = true;
      this.index = 0;
      this.interval = 200;
      this.lastTime = 0;
      this.finished = false;
      this.continueFrom = 0;
    }

    AnimationStep.prototype.getCurrent = function() {
      return this.steps[this.index];
    };

    AnimationStep.prototype.finish = function() {
      this.finished = true;
      return this.notify("animation-finished", null);
    };

    AnimationStep.prototype.update = function(dt) {
      if (!this.finished) {
        if (this.steps.length > 1) {
          this.lastTime += dt;
          if (this.lastTime > this.interval) {
            this.lastTime = this.lastTime % this.interval;
            if (this.index >= this.steps.length - 1) {
              if (this.continous) {
                return this.index = this.continueFrom;
              } else {
                return finish();
              }
            } else {
              return this.index++;
            }
          }
        } else {
          return this.index = 0;
        }
      }
    };

    return AnimationStep;

  })(Moshu.Observable);

  Moshu.Canvas2D.Components.Hero = (function() {

    function Hero(world, position, height) {
      var CHANGING_DIRECTION, JUMPING, MOVING, RESTING;
      this.world = world;
      this.position = position;
      this.height = height;
      this.statuses = [RESTING = "resting", MOVING = "moving", JUMPING = "jumping", CHANGING_DIRECTION = "changing-direction"];
      this.status = this.statuses.RESTING;
      this.direction = 0;
      this.speed = 150;
      this.image = new Moshu.Canvas2D.CanvasImage("./images/sprites/prince.png", this.position.x(), this.position.y(), 0, this.height);
      this.image.velocity(new Moshu.Canvas2D.Vector(this.speed, 0));
      this.image.registerSprite("stopped", 336, 15, 36, 84);
      this.image.registerSprite("run1", 32, 138, 44, 84);
      this.image.registerSprite("run2", 81, 138, 44, 84);
      this.image.registerSprite("run3", 134, 138, 44, 84);
      this.image.registerSprite("run4", 184, 138, 54, 84);
      this.image.registerSprite("run5", 235, 138, 54, 84);
      this.image.registerSprite("run6", 293, 138, 54, 84);
      this.image.registerSprite("run7", 353, 138, 65, 84);
      this.image.registerSprite("run8", 419, 138, 65, 84);
      this.image.registerSprite("run9", 488, 138, 50, 84);
      this.image.registerSprite("run10", 540, 138, 50, 84);
      this.image.registerSprite("run11", 600, 138, 65, 84);
      this.image.registerSprite("run12", 674, 138, 60, 84);
      this.image.registerSprite("run13", 734, 138, 45, 84);
      this.imagePainter = new Moshu.Canvas2D.ImagePainter(this.world.context, this.image);
      this.world.createShape(this.image, this.imagePainter);
      this.stop();
      window.Moshu.AnimationManager.bind(this, this.timeUpdate);
    }

    Hero.prototype.stop = function() {
      this.animation = new Moshu.Canvas2D.Components.AnimationStep();
      this.animation.flip = false;
      this.animation.steps = [0];
      this.animation.continous = false;
      this.direction = 1;
      return this.status = this.statuses.RESTING;
    };

    Hero.prototype.startMoving = function() {
      if (this.direction === 1) {
        this.animation = new Moshu.Canvas2D.Components.AnimationStep();
        this.animation.flip = true;
        this.animation.steps = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
        this.animation.continueFrom = 6;
        this.animation.interval = 300 - this.speed;
        if (this.animation.interval < 50) {
          this.animation.interval = 50;
        }
        return this.status = this.statuses.MOVING;
      }
    };

    Hero.prototype.jump = function() {
      return this.status = this.statuses.JUMPING;
    };

    Hero.prototype.changeDirection = function() {
      return this.status = this.statuses.CHANGING_DIRECTION;
    };

    Hero.prototype.timeUpdate = function(dt) {
      var spriteIndex;
      if (this.animation) {
        this.animation.update(dt);
        spriteIndex = this.animation.getCurrent();
        return this.image.selectSpriteByIndex(spriteIndex);
      }
    };

    return Hero;

  })();

}).call(this);
